package model

import (
	"fmt"
	"github.com/jinzhu/gorm"
)
type Car_half_apply_extend  struct {
	Applyid   int64
	Userid   int64
}


type Car_half_apply struct {
	ID   int64
	Applyid string `gorm:"default:'galeone'"`
	Dispose_userid  int64
	Ty  int64
}

type Orderqueue struct {
	Id   int64
	Name   string
}

type Seatqueue struct {
	Id   int64
	Name   string
}

func FdGetNewOrders() ([]*Car_half_apply,error) {
	applys,err := getNewOrdersData()

	if err != nil {
		return nil, err
	}
	for _, v := range applys {
		//applys[index].Ty =123 //两种赋值方式
		v.Ty =12311
	}
	//fmt.Println(applys)

	return applys,nil

	//if len(applys) == 0{  //未解决问题
	//		println(`这是个空切片`)
	//	}

	//var newMap map[int]string
	//newMap = make(map[int]string)

	//var archives []*Car_half_apply
	//var archive Car_half_apply
	for _, value := range applys {
		fmt.Println(value.Applyid)
		//value.Ty =123
		//archives.sd:=123
		//archive.Ty=1

		//fmt.Println(reflect.TypeOf(value))

		//fmt.Println(value)
		//newMap[key] = value
	}


	return applys,nil
}


func getNewOrdersData() ([]*Car_half_apply,error){
	db, _ := gorm.Open("mysql", "root:123456@/dev?charset=utf8&parseTime=True&loc=Local")
	db.SingularTable(true)
	rows,err :=db.Table("car_half_apply").Select("applyid,userid").Rows()
	if err != nil {
		return nil, err
	}
	var car_half_applys []*Car_half_apply
	defer rows.Close()
	for rows.Next() {
		var car_half_apply Car_half_apply
		var applyid int
		var userid int
		rows.Scan(&applyid,&userid)
		car_half_apply.Applyid = "12312312"
		car_half_applys = append(car_half_applys, &car_half_apply)
	}
	return car_half_applys,nil
}


/**
 * 对订单进行分组
 * 1、失效、驳回订单
 * 2、非时效订单
 * 3、时效订单
 *
 * @param $ordersList
 */
func GroupOrders(data []*Car_half_apply)  map[string]string{
	datamap := make(map[string]string)
	for i, v := range data {
		datamap["pdata"]=v.Applyid
		datamap["pdat"]=v.Applyid
		fmt.Println(i,v)
	}
	return datamap
}
//1、失效、驳回直接挂起到原处理人
func DispatchGiveback(data map[string]string) error  {
	//处理订单逻辑
	if data["pdat"] =="115203"{
		fmt.Println(111)
		return nil
	}
	fmt.Println(123)
	fmt.Println(data)
	//if(!$ordersList) return;

	return nil
}

func DispatchQueue(m string)  error{
	if m ==""{
		return nil
	}
	SetOrderDetail(m)
	fmt.Println(m)
	fmt.Println(123321)
	return nil
}
//插入队列数据库 还没写
func SetOrderDetail(m string) error  {
	return nil
}

func GetOrderQueue() []*Orderqueue  {
	db, _ := gorm.Open("mysql", "root:123456@/dev?charset=utf8&parseTime=True&loc=Local")
	db.SingularTable(true)
	var orderQueue []*Orderqueue
	db.Find(&orderQueue)
	return orderQueue
}

func GetSeatQueue() []*Seatqueue  {
	db, _ := gorm.Open("mysql", "root:123456@/dev?charset=utf8&parseTime=True&loc=Local")
	db.SingularTable(true)
	var seatQueue []*Seatqueue
	db.Find(&seatQueue)
	return seatQueue
}



